﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using GameCore;
using HandlingInput;

namespace GameObject
{
    public class ActionBoard: Window
    {
        List<ActionButton> l_ActionBution;
        Point size;


        public override event Window.GetEvent Enter;
        public override event Window.GetEvent Hover;
        public override event Window.GetEvent Normal;

        public Point Size
        {
            get { return size; }
            set { size = value; }
        }
        public ActionBoard(Point size)
        {
            l_ActionBution = new List<ActionButton>();
            Size = size;

        }
        public override bool Init(int _numberSprite, string[] strResource, string textDisplay, SpriteFont font)
        {
            TextDisplay = textDisplay;
            Font = font;
            _nsprite = _numberSprite;
            _sprite = new Sprite[_nsprite+1];
            _lsprite = new List<Vector2>();


            for (int i = 0; i < _nsprite; i++)
            {
                Texture2D[] texture = new Texture2D[strResource.Length];
                for (int j = 0; j < strResource.Length; j++)
                {
                    texture[j] = GlobalDefine.Content.Load<Texture2D>(@"Pictures/" + strResource[j]);
                }
                _sprite[i] = new Sprite(0, 0, 0, 0, texture);
            }

            Texture2D[] tt2d = new Texture2D[1];
            tt2d[0] = GlobalDefine.Content.Load<Texture2D>(@"Pictures/" + TextDisplay);
            _sprite[1] = new Sprite(0, 0, 0, 0, tt2d);
            _nsprite += 1;

         
            return true;
        }

        void MessageSender_SetActionButton(object sender, object l_action)
        {
            this.AddAction((List<ActionButton>)l_action);
        }
        public void Clear()
        {
            l_ActionBution.Clear();
        }

        public void AddAction(List<ActionButton> l_ab)
        {
            l_ActionBution.Clear();

            for (int i = 0; i < l_ab.Count; i++)
                l_ActionBution.Add((ActionButton) l_ab[i].Clone());
            //l_ActionBution = l_ab;
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            base.Draw(gameTime, spriteBatch);

            for (int i = 0; i < l_ActionBution.Count; i++)
            {
                l_ActionBution[i].Draw(gameTime, spriteBatch);
            }
        }
        public override void Update(GameTime gameTime)
        {
            

            this.Update(gameTime, X, Y, Width, Height);
            
        }
        public override void Update(GameTime gameTime, float x, float y, int w, int h)
        {

            MessageSender.SetActionButton += new MessageSender.GetEvent(MessageSender_SetActionButton);

            this.X = x;
            this.Y = y;
            this.Width = w;
            this.Height = h;

            EventHandle.Update();

            _lsprite.Clear();
 
            // Board Update
            if (EventHandle.MouseOver(new Rectangle((int)this.X, (int)this.Y, (int)this.Width, (int)this.Height)))
            {
                _lsprite.Add(new Vector2(0, s_hover));

                GetEvent handle = Hover;

                if (handle != null)
                    handle(this, GlobalDefine.now_gamestate);
            }
            else
            {
                _lsprite.Add(new Vector2(0, s_normal));

                GetEvent handle = Normal;

                if (handle != null)
                    handle(this, GlobalDefine.now_gamestate);
            }
            

            // Button upadte
            for (int i = 0; i < l_ActionBution.Count; i++)
            {
                l_ActionBution[i].Update(gameTime, x + (i % Size.X) * (w / Size.X),
                    y + ((i/Size.X) % Size.Y) * (h / Size.Y), w / Size.X, h / Size.Y);
            }


            base.Update(gameTime, x, y, w, h);
            _lsprite.Add(new Vector2(1, 0));
            this._sprite[1].Update(gameTime, 0, x, y, 100, 50);
        }

        
        public override VisibleGameObject Clone()
        {
            VisibleGameObject newObj = new ActionBoard(Size);

            newObj._nsprite = this._nsprite;
            ((ActionBoard)newObj).TextDisplay = this.TextDisplay;
            ((ActionBoard)newObj).Font = this.Font;
            ((ActionBoard)newObj).l_ActionBution = this.l_ActionBution;
            ((ActionBoard)newObj).Size = this.Size;

            newObj._sprite = new Sprite[_nsprite];
            for (int i = 0; i < _nsprite; i++)
            {
                newObj._sprite[i] = _sprite[i].Clone();
            }
            newObj.X = this.X;
            newObj.Y = this.Y;
            newObj.Width = this.Width;
            newObj.Height = this.Height;
            newObj._lsprite = new List<Vector2>();

            

            return newObj;
        }
    }
}
